Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice_things/fs/canonicalize.sh

canonicalize

Since 0.3.0 · Source

import "{ canonicalize }" from nice_things/fs/canonicalize.sh

Synopsis
canonicalize <out_var> <pathname>

Configuration

Description
Get the resolved absolute path name; all but the last component must exist. This function tries to closely emulate the default behavior of the realpath utilities in Busybox and GNU coreutils, which was standardized in POSIX-2024 as realpath -E.

Note

This is not a pure sh implementation because it depends on the external ls utility to read the target of a symlink. This works consistently because the output of ls is strictly specified in POSIX, but it means this function can be slower than most other functions in the framework. The slow branch is only reached when the last component of <pathname> is a symlink.

Options

Operands

  • <out_var>: Output variable; the result will be written to this variable.
  • <pathname>: A path name.

Stdin

Stdout

Stderr
log_debug.

Exit status

  • 0: Successful completion.
  • 23: No such file or directory.
  • 28: Too many levels of symbolic links.

Abort

Usage examples

# Get the real path to the current program
canonicalize program_path "$0" || {
	case $? in
	23) log_error "No such file or directory" ;;
	28) log_error "Too many levels of symbolic links" ;;
	*) log_error "Unexpected error" ;;
	esac
}